package cn.com.api.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.api.entity.ApiJuan;
import cn.com.api.service.ApiJuanService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "juan/")
public class ApiJuanController {

	@Autowired
	private ApiJuanService apiJuanService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiJuanController.class);

	@RequestMapping(value = "selectByUid")
	public void selectByUid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String uid = request.getParameter("uid");
		String token = request.getParameter("token");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(uid, token, map)) {
				List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
				if (uid != null && !"".equals(uid)) {
					res = apiJuanService.selectByUserId(uid);
				}
				map.put("datas", res);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 获取商家优惠券使用情况
	@RequestMapping(value = "getJuanByShopId")
	public void getJuanByShopId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String shopid = request.getParameter("shopid");
		String token = request.getParameter("token");
		Map<String, Object> map = new HashMap<String, Object>();

		List<Map<String, String>> res = new ArrayList<Map<String, String>>();
		try {
			// if (checkService.checkToken(uid, token, map)) {
			res = apiJuanService.getJuanByShopId(shopid);
			map.put("datas", res);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
			// }
		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 获取全城优惠券列表，通过用户ID
	@RequestMapping(value = "getJuanListByUserid")
	public void getJuanListByUserid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String uid = request.getParameter("uid");
		String token = request.getParameter("token");
		Map<String, Object> map = new HashMap<String, Object>();

		List<Map<String, String>> res = new ArrayList<Map<String, String>>();
		try {
			if (checkService.checkToken(uid, token, map)) {
				res = apiJuanService.getJuanListByUserid(uid);
				map.put("datas", res);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	@RequestMapping(value = "selectByArea")
	public void selectByArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String cityid = request.getParameter("cityid");
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, String>> res = new ArrayList<Map<String, String>>();
		res = apiJuanService.selectByArea(cityid, "1");
		map.put("datas", res);
		if (res.size() > 0) {
			map.put("error", "0000");
			map.put(Utils.MESSAGE, "操作成功");
		} else {
			map.put("error", "0000");
			map.put(Utils.MESSAGE, "没有数据！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 查询地区可用全城优惠券
	@RequestMapping(value = "selectQuanJuansByArea")
	public void selectQuanJuansByArea(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String cityid = request.getParameter("cityid");
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, String>> res = new ArrayList<Map<String, String>>();
		res = apiJuanService.selectQuanJuansByArea(cityid);
		map.put("datas", res);

		if (res.size() > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "获取成功");
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "没有数据！");
		}
		//
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 领取优惠券
	@RequestMapping(value = "receiveJuan")
	@Transactional
	public @ResponseBody Map<String, Object> receiveJuan(HttpServletRequest request, HttpServletResponse response) {
		String uid = request.getParameter("uid");
		String juanid = request.getParameter("juanid");
		String token = request.getParameter("token");
		Map<String, Object> res = new HashMap<String, Object>();
		ApiJuan ajuan = new ApiJuan();
		try {
			if (checkService.checkToken(uid, token, res)) {
				if (apiJuanService.searIsEnable(uid, juanid) == 0) {
					ajuan = apiJuanService.selectJuanByCid(Integer.parseInt(juanid));
					if (Integer.parseInt(ajuan.getCanReceive()) > 0) {
						if (apiJuanService.receiveJuan(uid, juanid, null)) {
							apiJuanService.updateCanReceive(juanid);
							res.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
							res.put(Utils.MESSAGE, "领取成功！");
						} else {
							res.put("error", "-9999");
							res.put(Utils.MESSAGE, "领取失败！");
						}
					} else {
						res.put("error", "-9999");
						res.put(Utils.MESSAGE, "已领完！");
					}
				} else {
					res.put("error", "2222");
					res.put(Utils.MESSAGE, "不能重复领取！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			res.put(Utils.MESSAGE, "系统异常！");
		}
		return res;

	}

	// 使用优惠卷
	@RequestMapping(value = "useJuan")
	public @ResponseBody Map<String, Object> useJuan(HttpServletRequest request, HttpServletResponse response) {
		String uid = request.getParameter("uid");
		String juanid = request.getParameter("juanid");
		String shopid = request.getParameter("shopid");

		String token = request.getParameter("token");

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			/* if (checkService.checkToken(uid, token, map)) { */
			boolean ret_bool = apiJuanService.useJuan(Integer.parseInt(juanid), uid, shopid);
			if (ret_bool) {
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "使用成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "优惠券与商家不匹配！");
			}
			/*
			 * }else{ map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			 * map.put(Utils.MESSAGE, "系统异常！"); }
			 */
		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		return map;
	}

	@RequestMapping(value = "payJuan")
	public @ResponseBody Map<String, Object> payJuan(HttpServletRequest request, HttpServletResponse response) {
		String uid = request.getParameter("uid");
		String cityid = request.getParameter("cityid");
		String shopid = request.getParameter("shopid");
		String products = request.getParameter("products");
		String money = request.getParameter("money");
		String token = request.getParameter("token");

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (checkService.checkToken(uid, token, map)) {
				apiJuanService.payJuan(money, new String[] { cityid, "-1" }, new String[] { shopid, "-1" }, uid, map,
						products);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		return map;
	}

}
